package com.aly.beanValidate.core.validator.impl;

import com.aly.beanValidate.core.FieldDefinition;
import com.aly.beanValidate.core.GlobalConfigs;
import com.aly.beanValidate.core.Util;
import com.aly.beanValidate.core.validator.CheckValidator;
import com.aly.beanValidate.core.validator.Name;
import com.aly.beanValidate.i.BeanInfo;
import com.aly.beanValidate.i.FileInfo;
import com.aly.beanValidate.i.FileInfos;
import org.dom4j.Element;

@Name("Range")
public class RangeValidate extends CheckValidator {
    private long max;
    private long min;

    public RangeValidate(Element element, GlobalConfigs globalConfigs) {
        super(element, globalConfigs);
        max = Long.parseLong(Util.attr(element, "max"));
        min = Long.parseLong(Util.attr(element, "min"));
    }

    @Override
    public StringBuffer doValidate(FieldDefinition field, Object val, BeanInfo bean, FileInfo fileInfo, FileInfos fileInfos) {
        long l = ((Number) val).longValue();
        if (l < max && l > min) {
            return null;
        }
        return new StringBuffer().append("不在指定范围:[").append(min).append(",").append(max).append("]");
    }
}
